package com.leyantech.ecp.agenx.web.di;

import com.leyantech.ecp.agenx.common.converter.input.DialogueQuestionConverter;
import com.leyantech.ecp.agenx.common.converter.output.DialogueReplyConverter;
import com.leyantech.ecp.agenx.common.di.HelaModule;
import com.leyantech.ecp.agenx.common.di.JWTAuthProvider;
import com.leyantech.ecp.agenx.web.converter.input.PinDuoDuoBlockingQuestionConverter;
import com.leyantech.ecp.agenx.web.converter.output.PinDuoDuoBlockingQuestionReplyConverter;
import com.leyantech.ecp.agenx.web.filter.AuthFilter;

import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import io.vertx.ext.auth.jwt.JWTAuth;
import org.reflections.Reflections;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-16.
 */
public class WebModule extends AbstractModule {

  @Override
  protected void configure() {

    install(new HelaModule());

    bind(DialogueReplyConverter.class)
        .to(PinDuoDuoBlockingQuestionReplyConverter.class)
        .asEagerSingleton();

    bind(DialogueQuestionConverter.class)
        .to(PinDuoDuoBlockingQuestionConverter.class)
        .asEagerSingleton();

    bind(JWTAuth.class).toProvider(JWTAuthProvider.class).asEagerSingleton();

  }

  @Singleton
  @Provides
  public List<AuthFilter> provideAuthFilters(Injector injector) {
    return new Reflections(AuthFilter.class)
        .getSubTypesOf(AuthFilter.class)
        .stream()
        .map(injector::getInstance)
        .collect(Collectors.toList());
  }

}
